home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_12 / allison / string.cpp < prev    next >
C/C++ Source or Header  |  1993-10-10  |  3KB  |  137 lines

  1. LISTING 2 - String Class Implementation
  2. // string.cpp
  3.  
  4. #include <iostream.h>
  5. #include <iomanip.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include "string.hpp"
  9.  
  10. string::string(const char *s, size_t n)
  11. {
  12.     assert(s);
  13.     clone(s,n);
  14. }
  15.  
  16. string& string::operator=(const string& s)
  17. {
  18.     if (this != &s)
  19.     {
  20.         delete [] data;
  21.         clone(s.data,s.count);
  22.     }
  23.     return *this;
  24. }
  25.  
  26. string& string::operator+=(const string& s)
  27. {
  28.     assert(count + s.count < NPOS);
  29.     if (s.count > 0)
  30.     {
  31.         int new_count = count + s.count;
  32.         char *buf = new char[new_count + 1];
  33.  
  34.         memcpy(buf,data,count);
  35.         memcpy(buf+count,s.data,s.count);
  36.         buf[new_count] = '\0';
  37.         delete [] data;
  38.         data = buf;
  39.         count = new_count;
  40.     }
  41.     return *this;
  42. }
  43.  
  44. size_t string::find(const string& s, size_t pos) const
  45. {
  46.     assert(pos < count);
  47.     char *p = strstr(data,s.c_str());
  48.     if (p)
  49.         return pos + (p - data);
  50.     return NPOS;
  51. }
  52.  
  53. size_t string::find_first_of(const string& s, size_t pos) const
  54. {
  55.     assert(pos < count);
  56.     char *p = strpbrk(data+pos,s.data);
  57.     if (p)
  58.         return p - data;
  59.     return NPOS;
  60. }
  61.  
  62. size_t string::find_first_not_of(const string& s, size_t pos)
  63. const
  64. {
  65.     assert(pos < count);
  66.     for (size_t i = pos; i < count; ++i)
  67.         if (strchr(s.data,data[i]) == NULL)
  68.             return i;
  69.     return NPOS;
  70. }
  71.  
  72. string string::substr(size_t pos, size_t n) const
  73. {
  74.     assert(pos <= count);
  75.     if (n > count - pos)
  76.         n = count - pos;
  77.  
  78.     if (n > 0)
  79.         return string(data+pos,n);
  80.     else
  81.         return string();    // Empty string
  82. }
  83.  
  84. ostream& operator<<(ostream& os, const string& s)
  85. {
  86.     os.write(s.data,s.count);
  87.     return os;
  88. }
  89.  
  90. istream& operator>>(istream& is, string& s)
  91. {
  92.     const size_t BUFSIZ = 256;
  93.     char buf[BUFSIZ];
  94.  
  95.     is >> setw(BUFSIZ) >> buf;
  96.     s.clone(buf,strlen(buf));
  97.     return is;
  98. }
  99.  
  100. void string::put_at(size_t pos, char c)
  101. {
  102.     assert(pos <= count);
  103.     if (pos < count)
  104.         data[pos] = c;
  105.     else
  106.     {
  107.         // Append character the lazy way
  108.         char buf[2];
  109.         buf[0] = c;
  110.         buf[1] = '\0';
  111.         operator+=(buf);
  112.     }
  113. }
  114.  
  115. int operator==(const string& s1, const string& s2)
  116. {
  117.     return s1.count == s2.count &&
  118.       memcmp(s1.data,s2.data,s1.count) == 0;
  119. }
  120.  
  121. int operator!=(const string& s1, const string& s2)
  122. {
  123.     return s1.count != s2.count ||
  124.       memcmp(s1.data,s2.data,s1.count);
  125. }
  126.  
  127. // Private function
  128. void string::clone(const char *s, size_t n)
  129. {
  130.     // Assumes "data" needs to be allocated
  131.     assert(s != NULL);
  132.     size_t len = strlen(s);
  133.     count = (n >= len) ? len : n;
  134.     data = new char[count + 1];
  135.     strncpy(data,s,count)[count] = '\0';
  136. }
  137.